package cn.donting.jme.handle;

import cn.donting.jme.EditorContent;
import cn.donting.jme.event.file.OpenFileEvent;
import cn.donting.jme.expansion.file.OpenFile;
import cn.donting.jme.ioc.Bean;
import cn.hutool.core.io.FileUtil;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

@Slf4j
@Bean
@EvenHandle
public class FileEventHandle {
    @Subscribe
    public void openFile(OpenFileEvent fileEvent) {
        File file = fileEvent.getFile();
        if (!file.isFile()) {
            return;
        }
        String extName = FileUtil.extName(file);
        List<OpenFile> openFiles = EditorContent.getIocContainer().getBeansByTypes(OpenFile.class);
        for (OpenFile openFile : openFiles) {
            if (openFile.isOpen(file,extName)) {
                try {
                    openFile.open(file);
                } catch (Exception e) {
                    log.warn(e.getMessage(),e);
                }
            }
        }
    }



}
